<?php
/**
 * Accessed from view.php. This performs various actions on items within
 * a container (show, hide, delete).
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package resourcepage
 *//** */
  
 // This page performs actions on items within a container.
  
require_once("module_resourcepage.php");
require_once("resourcepage_item.php");
include_once($CFG->dirroot. '/local/sitesetup/moodle_setup_actions.php');
include_once($CFG->dirroot.'/mod/resource/type/rpfilelink/resource.class.php');
$DASHBOARD_COUNTER=DASHBOARD_RESOURCEPAGE_EDIT;

$module=module_resourcepage::$inst;
$id=required_param('id',PARAM_INT);    // Course Module ID
$container=required_param('container',PARAM_INT);
$item=required_param('item',PARAM_ALPHANUM);
$extensiontype=optional_param('extensiontype',PARAM_ALPHA);
$scrollpos=optional_param('scrollpos',0,PARAM_INT);
$action = isset($_POST['actionshow_x']) ? "show"
        : (isset($_POST['actionhide_x']) ? "hide"
        : (isset($_POST['actiondelete_x']) ? "delete"
        : (isset($_POST['actioncopy_x']) ? "copy"
        : (isset($_POST['actioncancel_x']) ? "cancel"
        : (isset($_POST['main_x']) ? "copytomain"
        : null))))); // Brackets are necessary  
if(is_null($action)) {
    error("Action command required its null ");
}


// Basic security checks
$module->obtain_view_info($id,null,$cm,$course,$resourcepage);
$module->verify_edit_action($cm);

// Item or subcontainer? 
if(strpos($item,'c')===0) {
    $subcontainer=substr($item,1);
    
    if($subcontainer){
        // Verify item is within that container and container is within that module
        $module->verify_container_subcontainer_editable(
        array($resourcepage->containerid,$resourcepage->deletedcontainerid),
        $container,$subcontainer);
        // Carry out action
        switch($action) {
             case 'delete' :
                // Really delete it if it's in the deleted container
                if($container == $resourcepage->deletedcontainerid) {
                    $tw=new transaction_wrapper();
                    
                    // Shared section? If so we don't delete it, only the reference
                    $rs=$module->do_sql('SELECT resourcepageid FROM modprefix_containers WHERE id='.$subcontainer);
                    if($rs->fields['resourcepageid']!=$resourcepage->id) {
                        $module->delete_container_subcontainer($container,$subcontainer);
                    } else {
                        // Belongs to this page, so delete the container and its items
                        $module->delete_container_with_items($subcontainer);
                    }
                    $tw->commit();
                } else { // Otherwise just move it to deleted container
                    $module->move_container_subcontainer($container,$subcontainer,$resourcepage->deletedcontainerid);
                }            
                break;
             case 'show' :
                  $module->set_container_subcontainer_hidden($container,$subcontainer,false);
                  break;
             case 'hide' :
                  $module->set_container_subcontainer_hidden($container,$subcontainer,true);;
                  break;
             case 'copy' :
                 // Store copied item id in session
                 $_SESSION['copieditem'] = $item;
                 break;   
             default:
                 error('[BUG/HACK] Unexpected action: '.htmlspecialchars($action));
         }
    
    }else {
        // container
        $module->verify_container_editable(array($resourcepage->containerid,$resourcepage->deletedcontainerid),$container);
        // Carry out action
        switch($action) {
            case 'cancel' :
            // Really delete it as it.is not yet been properly created
            if($container != $resourcepage->deletedcontainerid) {
                $tw=new transaction_wrapper();
                
                // Shared section? If so we don't delete it, only the reference
                $rs=$module->do_sql('SELECT resourcepageid FROM modprefix_containers WHERE id='.$container);
                if($rs->fields['resourcepageid']==$resourcepage->id) {
                    // Belongs to this page, so delete the container and its items
                    $module->delete_container_with_items($container);
                }
                $tw->commit();
            } // endif
            break;
            default:
            error('[BUG/HACK] Unexpected action: '.htmlspecialchars($action));
        } // end-switch statement

    } // endif 
    
} else {
    // Verify item is within that container and container is within that module
    $module->verify_container_item_editable(
        array($resourcepage->containerid,$resourcepage->deletedcontainerid),
        $container,$item);
        
    // Get item information 
    $itemfields=$module->get_item_fields($item);

    // Carry out action
    switch($action) {
        case 'delete' :
            // Really delete it if it's in the deleted container
            if($container == $resourcepage->deletedcontainerid) {
                $module->delete_item($item);
            } else { // Otherwise just move it to deleted container
                $module->move_container_item($container,$item,$resourcepage->deletedcontainerid);
            }            
            break;
        case 'show' :
            $module->set_container_item_hidden($container,$item,false);
            break;
        case 'hide' :
            $module->set_container_item_hidden($container,$item,true);
            break;
        case 'copy' :
            // Store copied item id in session
            $_SESSION['copieditem'] = $item;
            break;
        case 'copytomain' :
            // Store copied item id 
            $resitem = $module->get_container_item(
                    array($resourcepage->containerid), $item);
            $title = $resitem->get_field('title');
            $cmid = resource_rpfilelink::add_rpfile_link_to_section0($course->id,$title,$item);
            // Redirect to the course page im move mode, so the new thing can be dropped into place.
            redirect($CFG->wwwroot . '/course/mod.php?id=' . $course->id . '&copy=' . $cmid.
                    '&sr=0&sesskey='.sesskey());
            break;    
        default:
            error('Unexpected action: '.htmlspecialchars($action));
    }
}
 
// Log and return to view
add_to_log($course->id, "resourcepage", "containeritem", "view.php?id=$cm->id",
    "Changed #$item: $action",$cm->id);

redirect('view.php?id='.$cm->id.($scrollpos ? '&scrollpos='.$scrollpos : ''));
 
?>